package com.heihei.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.heihei.common.page.Condition;
import com.heihei.common.page.Query;
import com.heihei.common.response.ResponseVO;
import com.heihei.common.tree.ForestNodeMerger;
import com.heihei.comstant.CommonMessageConstant;
import com.heihei.entity.HelloWorldEntity;
import com.heihei.mapper.HelloWorldMapper;
import com.heihei.service.HelloWorldService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heihei.vo.HelloWorldTreeVO;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duanhengfei
 * @since 2021-02-24
 */
@Service
public class HelloWorldServiceImpl extends ServiceImpl<HelloWorldMapper, HelloWorldEntity> implements HelloWorldService {

    /**
     * 方法描述: 添加
     *
     * @param entity
     * @Return {@link ResponseVO< Long>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 10:41:05
     */
    @Override
    public ResponseVO<Long> add(HelloWorldEntity entity) {
        entity.setId(null);
        return this.save(entity) ? ResponseVO.dealSuccess(entity.getId()) : ResponseVO.dealFail();
    }

    /**
     * 方法描述: 修改
     *
     * @param entity
     * @Return {@link ResponseVO< String>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:14:49
     */
    @Override
    public ResponseVO<String> edit(HelloWorldEntity entity) {
        if(entity.getId() == null) {
            return ResponseVO.paramLess(CommonMessageConstant.EMPTY_ID);
        }
        return this.updateById(entity) ? ResponseVO.dealSuccess() : ResponseVO.dealFail();
    }

    /**
     * 方法描述: 列表查询
     *
     * @param param
     * @param query
     * @Return {@link ResponseVO< IPage< HelloWorldEntity>>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:19:18
     */
    @Override
    public ResponseVO<IPage<HelloWorldEntity>> listConditional(HelloWorldEntity param, Query query) {
        LambdaQueryWrapper<HelloWorldEntity> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotEmpty(param.getName())) {
            queryWrapper.like(HelloWorldEntity::getName, param.getName());
        }
        if(param.getAge() != null) {
            queryWrapper.eq(HelloWorldEntity::getAge, param.getAge());
        }
        if(param.getPid() != null) {
            queryWrapper.eq(HelloWorldEntity::getPid, param.getPid());
        }
        IPage<HelloWorldEntity> pageResult = this.page(Condition.getPage(query), queryWrapper);
        return ResponseVO.dealSuccess(pageResult);
    }

    /**
     * 方法描述: 获取详情
     *
     * @param id
     * @Return {@link ResponseVO< HelloWorldEntity>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:27:16
     */
    @Override
    public ResponseVO<HelloWorldEntity> get(Long id) {
        HelloWorldEntity entity = this.getById(id);
        return entity == null ? ResponseVO.recordNotExists() : ResponseVO.dealSuccess(entity);
    }

    /**
     * 方法描述: 删除
     *
     * @param id
     * @Return {@link ResponseVO< String>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:28:59
     */
    @Override
    public ResponseVO<String> delete(Long id) {
        return this.removeById(id) ? ResponseVO.dealSuccess() : ResponseVO.recordNotExists();
    }

    /**
     * 方法描述: 批量删除
     *
     * @param idList
     * @Return {@link ResponseVO< String>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:30:04
     */
    @Override
    public ResponseVO<String> deleteBatch(List<Long> idList) {
        if(CollectionUtils.isEmpty(idList)) {
            ResponseVO.paramLess(CommonMessageConstant.EMPTY_ID);
        }
        return this.removeByIds(idList) ? ResponseVO.dealSuccess() : ResponseVO.recordNotExists();
    }

    /**
     * 方法描述: 查询树结构
     *
     * @param
     * @Return {@link ResponseVO< List< HelloWorldEntity>>}
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:31:38
     */
    @Override
    public ResponseVO<List<HelloWorldTreeVO>> tree() {
        List<HelloWorldTreeVO> dataList = this.baseMapper.getTreeInfoParam();
        //结果为空
        if(CollectionUtils.isEmpty(dataList)) {
            return ResponseVO.dealSuccess(new ArrayList<>());
        }
        List<HelloWorldTreeVO> resultTreeList = ForestNodeMerger.merge(dataList);
        //修正hasChildren和parentName
        verifyHasChildrenAndPName(resultTreeList);
        //返回查询结果
        return ResponseVO.dealSuccess(resultTreeList);
    }

    /**
     * 方法描述: 修正hasChildren和parentName
     *
     * @param resultTreeList
     * @Return
     * @throws
     * @author duanhengfei
     * @date 2021年02月24日 14:39:23
     */
    private void verifyHasChildrenAndPName(List<HelloWorldTreeVO> resultTreeList) {
        verifyHasChildrenAndPName(resultTreeList, null);
    }

    private void verifyHasChildrenAndPName(List<HelloWorldTreeVO> resultTreeList, String pName) {
        for (HelloWorldTreeVO child : resultTreeList) {
            if(StringUtils.isNotEmpty(pName)) {
                child.setParentName(pName);
            }
            if(CollectionUtils.isNotEmpty(child.getChildren())) {
                child.setHasChildren(true);
                verifyHasChildrenAndPName(child.getChildren(), child.getName());
            } else {
                child.setHasChildren(false);
            }
        }
    }
}
